const Base_Url = 'https://api-hmugo-web.itheima.net/api/public/v1/' // 基地址

import store from '../store'
// 接受对象作为参数,返回Promise对象

const request = ({
  url,
  method = 'GET',
  data = {},
  header = {},
  tip = '拼命加载中~~',
  isPullDown = false
}) => {
  return new Promise((resolve, reject) => {
    if (!isPullDown) {
      uni.showLoading({
        title: tip,
        mask: true
      })
    }

    // 携带token
    const token = store.getters['user/getToken']
    if (token) {
      header.Authorization = token
    }

    uni.request({
      url: `${Base_Url}${url}`, //仅为示例，并非真实接口地址。
      method,
      data,
      header,
      success: res => {
        resolve(res.data)
      },
      fail: err => {
        reject(err)
      },
      complete: () => {
        if (!isPullDown) {
          uni.hideLoading()
        }
      }
    })
  })
}

uni.$request = request
